'use strict';

const fs = require('fs');
const fsPromises = fs.promises;
const path = require('path');

/**
 * @param {Egg.Application} app - egg application
 */
module.exports = app => {
  const { router, controller, middleware } = app;

  const v1Router = router.namespace('/api/v1');
  const v1Controller = controller.v1;
  const v1RouterBasePath = path.resolve(__dirname, './routes/v1');

  const requireRouters = async (basePath, router, controller, middleware) => {
    const files = await fsPromises.readdir(basePath);
    files.forEach(file => {
      require(path.join(basePath, file))(router, controller, middleware);
    });
  };

  requireRouters(v1RouterBasePath, v1Router, v1Controller, middleware);
};
